package org.lisa.crm.service.impl;



import org.lisa.crm.common.util.JsonUtil;
import org.lisa.crm.common.util.Md5Util;
import org.lisa.crm.common.util.Result;
import org.lisa.crm.common.util.VerificationLoginUtil;
import org.lisa.crm.dao.UserDao;
import org.lisa.crm.model.User;
import org.lisa.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by admin on 2018/1/4.
 */
@Service
public class UserServiceImpl  implements UserService{

    @Autowired
    private UserDao userDao;

    //如果没有就不注入
    @Autowired(required = false)
    private HttpServletResponse response;


    @Autowired(required = false)
    private HttpSession session;

    @Override
    public Result userLogin(User user) {
        Result result=null;
        if(user.getUserName()==null||user.getUserPwd()==null){
            return Result.fail("请输入用户名或者密码");
        }

        String pwd= Md5Util.encode(user.getUserPwd());
        User user1 = userDao.queryUserByNameAndByPassword(user.getUserName(),pwd);
        if(user1!=null){
            String json= JsonUtil.parseObjectToString(user);
            VerificationLoginUtil.ULoginTools(response,session,json);
            result= Result.success("登录成功");

            //用户的权限
            List<String>  permission = userDao.queryPermissionByUid(user1.getId());
            session.setAttribute("userPermission",permission);
        }
        return result;
    }

    @Override
    public List<User> queryAllCustomerManager() {
        return userDao.queryAllCustomerManager();
    }
}
